Skip to content

Bind a trait's abstract methods after inheritance - #887

Closed
Firehed wants to merge 9 commits into
mainfrom
732/abstract-trait-methods
Closed

Firehed wants to merge 9 commits into
mainfrom
732/abstract-trait-methods

Conversation

@Firehed

@Firehed Firehed commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

A trait's abstract method no longer hides a method the class gets elsewhere. PHP binds a class's used traits in two passes around inheritance (php-src zend_do_link_class): concrete trait members before the class inherits from its parent, abstract trait methods after, and an abstract trait method only fills a name nothing else provides (zend_add_trait_method). The member resolver flattened every trait member ahead of the parent chain, so in

class P { public function shared(): string { return ''; } }
trait T { abstract public function shared(): string; }
class C extends P { use T; }

C::shared resolved to T's abstract method instead of P's.

Changes

  • MemberResolver::supertypes() lists a class's used traits twice: before the parent chain and after it, each tagged with a TraitBinding. Each trait is looked up once.
  • A used trait is resolved as a unit (traitMembers()): its own members win over those of traits it uses, as PHP composes a trait before it is used. Only then is each member assigned to a binding. So a trait whose own abstract method overrides a concrete one from a trait it uses still brings in an abstract method. Each trait is resolved once per walk, for the wanted name only.
  • A member bound after inheritance that meets a name the class's own level or parent chain already has is dropped, as PHP only checks its signature. The declaration list stays nearest-first, so protected access is still checked against the class that first declared the method.
  • A trait cycle (trait T { use T; }, or two traits using each other), which PHP rejects but code being edited can contain, brings in nothing more instead of recursing.
  • MemberKind::bindsBeforeInheritance() says which members bind first: everything except abstract methods.

Behavior

Hover, go to definition and signature help on such a method now show the inherited or concrete trait method rather than the abstract one; completion lists it once, as concrete.

Tests

MemberResolverTypeGraphTest gains a reflection check that every public method is abstract exactly when PHP says it is, over all its hierarchy fixtures, plus fixtures for: an abstract trait method beside a parent method, beside a concrete trait method, a trait whose own abstract method overrides a used trait, and an abstract trait method alone.

MemberResolverTest adds a trait cycle and protected access from a sibling to a parent method that a used trait redeclares abstract.

Same kind of trait-composition mismatch as the cases in #732; this one is not among those listed there. Two related cases already on main remain: an as alias of an abstract trait method overrides an inherited method, and insteadof is applied to abstract methods, which PHP binds without exclusions. They belong with #732.

🤖 Generated with Claude Code

Firehed and others added 5 commits October 29, 2026 14:03
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.71%. Comparing base (7faf80c) to head (31c9c0a).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #887   +/-   ##
=========================================
  Coverage     99.70%   99.71%           
- Complexity     1952     1963   +11     
=========================================
  Files           146      146           
  Lines          4459     4497   +38     
=========================================
+ Hits           4446     4484   +38     
  Misses           13       13           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Firehed and others added 4 commits October 29, 2026 14:17
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Firehed added a commit that referenced this pull request Oct 29, 2026
Which traits a class-like uses now has one answer, and it holds only
traits. `MemberResolver` turned every name written in `use` into a trait
edge, whatever that name turned out to be, and flattened its members
into the using class as the class's own. In code being edited `use`
often names a class, interface or enum, and its members, private ones
included, then counted as the using class's own. Alias resolution
separately read the written trait names itself and looked up whatever
class an alias named.

## Rules (php-src)

- `use` names only traits ("%s cannot use %s - it is not a trait",
`zend_do_link_class` in `Zend/zend_inheritance.c`).
- An `as` alias that names its trait draws only from a trait the class
uses ("Required Trait %s wasn't added to %s").

## Changes

- `MemberResolver::usedTraits()` is the one answer to which traits a
class-like uses, keeping only names that resolve to a trait. The walk
(`supertypes()`) and alias resolution (`findAliasSource()`) both read
it.

## Behavior

Only code PHP rejects changes: members of a class, interface or enum
named in `use` no longer appear on the class using it, and an alias
naming a trait the class does not use resolves to nothing.

## Not changed

A parent or interface of the wrong kind (`class C extends
SomeInterface`) is still followed. PHP rejects that declaration
outright, so there is no correct answer to match, and that class-like's
members stay attributed to it rather than to the class.

Alias resolution has no cycle guard of its own. A class using itself
with an alias used to recurse until PHP segfaulted; it no longer reaches
that path, which `MemberResolverTest` now pins. A trait using itself
with an alias still recurses without end. That is #889, kept as the
known-defect script `tests/EndToEnd/defects/889-trait-alias-cycle.php`
(ADR 0008).

## Why now

#887 resolves used traits as units, which showed that a class named in
`use`, including a class using itself, leaked that class's whole
hierarchy into the user. That PR is paused until this lands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@Firehed Firehed closed this in #890 Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant